﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using System.Xml;

public partial class Controls_Admin_ManageSettingsControl : System.Web.UI.UserControl
{
    Oplaco.Core.Configuration.SiteSettings settings;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            settings = Oplaco.Core.Configuration.SiteSettings.GetSharedSettings();
            ckbActivationRequired.Checked = settings.AdActivationRequired;
            txtMaxRunningAdDays.Text = settings.MaxAdRunningDays.ToString();
            ckbAllowUsersToEditAds.Checked = settings.AllowUsersToEditAds;
            txtMaxPhotos.Text = settings.MaxPhotosPerAd.ToString();
            txtSiteName.Text = settings.SiteName;
            txtContactEmail.Text = settings.SiteEmailAddress;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e) {


        Oplaco.Core.Configuration.SiteConfig config = new Oplaco.Core.Configuration.SiteConfig();
        config.AdActivationRequired = ckbActivationRequired.Checked ? true : false;
        config.AdminNotification = ddlSendNotifications.SelectedValue;
        config.AllowUsersToDeleteAdsInDB = rbnRemoveFromDatabase.Checked;
        config.AllowUsersToEditAds = ckbAllowUsersToEditAds.Checked;

        if(!String.IsNullOrEmpty(txtMaxRunningAdDays.Text)) config.MaxAdRunningDays = ushort.Parse(txtMaxRunningAdDays.Text);
        if(!String.IsNullOrEmpty(txtMaxPhotos.Text)) config.MaxPhotosPerAd = byte.Parse(txtMaxPhotos.Text);
        if(!String.IsNullOrEmpty(txtContactEmail.Text)) config.SiteEmailAddress = txtContactEmail.Text;

        if (ddlLanguage.SelectedValue == "User") config.SitePreferUserCulture = true;
        else config.SiteLanguage = ddlLanguage.SelectedValue;

        XmlTextWriter wtr = new XmlTextWriter(HttpContext.Current.Server.MapPath("~") + "/App_Data/site-config.xml", System.Text.Encoding.UTF8);
        XmlSerializer x = new XmlSerializer(config.GetType());
        x.Serialize(wtr, config);
        
    }
}